home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4922 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.5 KB  |  162 lines

  1. Newsgroups: comp.lang.c
  2. Path: comp.vuw.ac.nz!cscnews!not-for-mail
  3. From: b_brown@staff.cit.ac.nz (Brian Brown)
  4. Subject: Re: how to use bioscom in turbo C
  5. Message-ID: <2c7cc$7380.17@cscnews>
  6. Date: Sun, 11 Feb 1996 19:55:59 GMT
  7. Organization: Central Institute of Technology
  8. X-Newsreader: WinVN 0.99.2
  9. References: <4fimc0$ksj@ns2.emirates.net.ae>
  10. MIME-Version: 1.0
  11.  
  12. In article <4fimc0$ksj@ns2.emirates.net.ae>, kannan@emirates.net.ae says...
  13. >
  14. >I am trying to send information through the RS232 (com port) to a
  15. >burning machine. I know bioscom command is meant for RS232 interface.
  16. >Can someone give me an example of setting this port first (lets say
  17. >9600 baud, no parity, 1 stop bit, 8 char) & send the info to this
  18. >after checking the port is ready.
  19. >
  20. >thanx for any help.
  21. >
  22. RS232 BIOS CALLS
  23.  
  24. INT 14H This interrupt provides support for RS232 communications. The AH register value, on entry, 
  25. determines the function to be performed. 
  26.  
  27.         AH = 0 reset port, DX = 0 = com1
  28.                 return value in AL
  29.                         7,6,5 Baud rate 000 = 110 100 = 1200
  30.                         001 = 150 101 = 2400
  31.                         010 = 300 110 = 4800
  32.                         011 = 600 111 = 9600
  33.                         4,3 Parity (00,10=off) (01=odd, 11=even)
  34.                         2 Stops (0=One, 1=Two stops)
  35.                         1,0 Word Size (10=7,11=8)
  36.         AH = 1 xmit a character, character in AL
  37.         AH = 2 recieve a character, returns character in AL
  38.         AH = 3 return status
  39.  
  40.  
  41. Now, lets develop routines in C to program the rs232 card using the int 14 BIOS call, ie, the 
  42. bioscom() function in TurboC. 
  43.  
  44.         #include <dos.h>
  45.         int bioscom( cmd, byte, port )
  46.         int cmd;
  47.         char byte;
  48.         int port;
  49.         {
  50.                 union REGS regs;
  51.                 regs.x.dx = port;
  52.                 regs.h.ah = cmd;
  53.                 regs.h.al = byte;
  54.                 int86( 0x14, ®s, ®s );
  55.                 return( regs.x.ax );
  56.         }
  57.  
  58.  
  59. Now, lets develop routines to initialise the specified comport, and to transmit and recieve 
  60. characters, without resorting to using int 14h. These types of routines
  61. directly program the rs232 card, thus are ideal for embedded applications, ie, ROMMABLE code. 
  62.  
  63.         /*- Initiliase the RS232 serial card -*/
  64.         #define INP inportb
  65.         #define OUTP outportb
  66.         /* Defines for RS232 communications */
  67.         #define DLL 0 /* divisor latch low byte */
  68.         #define DLH 1 /* divisor latch high byte */
  69.         #define THR 0 /* transmit hold register */
  70.         #define RBR 0 /* recieve buffer register */
  71.         #define IER 1 /* interrupt enable register */
  72.         #define LCR 3 /* line control register */
  73.         #define MCR 4 /* modem control register */
  74.         #define LSR 5 /* line status register */
  75.         #define MSR 6 /* modem status register */
  76.         #define RTS 0x02 /* request to send */
  77.         #define CTS 0x10 /* clear to send */
  78.         #define DTR 0x01 /* data terminal ready */
  79.         #define DSR 0x20 /* data set ready */
  80.         #define RBF 0x01 /* bit 0 of LSR, rec buf full */
  81.         #define THRE 0x20 /* bit 5 of LSR, trans reg 0 */
  82.         #define DISINT 0x00 /* disable interrupts in IER */
  83.         #define ABRG 0x83 /* access baud rate generator */
  84.         /**/
  85.  
  86.         void rs232_init( com_port, baud rate, parity, stops, word_size )
  87.         int com_port, baud_rate, word_size, stops;
  88.         char *parity;
  89.         {
  90.                 unsigned int divisorh, divisorl, format, acia[2];
  91.                 int far *bios = 0x00400000l;
  92.                 acia[0] = *bios; /* pick up address of com1 routine */
  93.                 acia[1] = *(bios + 1); /* pick up address of com2 routine */
  94.                 OUTP(acia[com_port] + IER, DISINT ); /* disable ints */
  95.                 OUTP(acia[com_port] + LCR, ABRG ); /* access baud rate gen*/
  96.                 switch( baud_rate ) {
  97.                         /* rem case 75, 110, 135, 150, 200, 1800, 19200 */
  98.                         case 300 : divisorh = 01; divisorl = 0x80; break;
  99.                         case 600 : divisorh = 00; divisorl = 0xc0; break;
  100.                         case 1200 : divisorh = 00; divisorl = 0x60; break;
  101.                         case 2400 : divisorh = 00; divisorl = 0x30; break;
  102.                         case 4800 : divisorh = 00; divisorl = 0x18; break;
  103.                         case 9600 : divisorh = 00; divisorl = 0x0c; break;
  104.                         default: printf("\nrs232_init: Error: Baud rate invalid.\n");
  105.                                 return -1;
  106.                 } /* end of switch */
  107.                 OUTP(acia[com_port] + DLL, divisorl );
  108.                 OUTP(acia[com_port] + DLH, divisorh );
  109.                 format = 0; /* This sets bit 6 and 7 to zero */
  110.                 if( (strcmp( parity, "E" ) == 0) || (strcmp( parity, "O" ) == 0) ) {
  111.                         format = format  0x28; /* set bit 3 and 5 */
  112.                         if( strcmp( parity, "E" ) == 0 )
  113.                                 format = format  0x10; /* set bit 4 */
  114.                 }
  115.                 if( stops == 2 )
  116.                         format = format  0x04;
  117.                 switch( word_size ) {
  118.                         case 8 : format = format  0x03; break;
  119.                         case 7 : format = format  0x02; break;
  120.                         case 6 : format = format  0x01; break;
  121.                         case 5 : break;
  122.                         default: printf("\nrs232_init: Unsupported word length.\n");
  123.                                 return -1;
  124.                 } /* end of switch */
  125.                 OUTP(acia[com_port] + LCR, format );
  126.                 return 0;
  127.         }
  128.  
  129.         /* Transmit a single character to RS232 card -*/
  130.         void transmit( byte )
  131.         char byte;
  132.         {
  133.                 OUTP(acia[comport] + MCR, (RTS | DTR) ); /* assert RTS and DTR */
  134.                 while((INP(acia[comport] + LSR) & THRE)==0) /* trans reg empty? */
  135.                         ;
  136.                 OUTP(acia[comport] + THR, byte ); /* write character to THR */
  137.                 OUTP(acia[comport] + MCR, 0 );
  138.         }
  139.  
  140.         /* Receive a single character from RS232 card */
  141.         char receive()  {
  142.                 char byte;
  143.                 OUTP(acia[comport] + MSR, (RTS | DTR) );
  144.                 while((INP(acia[comport]+LSR)&RBF)==0) /* has Data arrived? */
  145.                         ;
  146.                 OUTP(acia[comport] + MCR,0); /* stop all data */
  147.                 byte = INP(acia[comport] + RBR); /* get byte RBR */
  148.                 return( byte );
  149.         }
  150.  
  151.  
  152.  
  153.  
  154. -- 
  155. Brian Brown
  156. Senior Lecturer, Computing
  157. Central Institute of Technology
  158. Trentham, Upper-Hutt
  159. New Zealand.
  160.  
  161.  
  162.